package io.renren.modules.maintenance.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.R;
import io.renren.modules.maintenance.entity.MGuest;
import io.renren.modules.maintenance.entity.MOrder;
import io.renren.modules.maintenance.mapper.MGuestMapper;
import io.renren.modules.maintenance.mapper.MOrderMapper;
import io.renren.modules.maintenance.service.impl.MDeviceCategoryServiceImpl;
import io.renren.modules.maintenance.service.impl.MGuestServiceImpl;
import io.renren.modules.maintenance.service.impl.MOrderServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author iweb
 * @since 2020-08-07
 */
@RestController
@RequestMapping("/order")
@Api(value = "订单相关测试")
public class MOrderController {

    @Autowired
    private MOrderServiceImpl mOrderService;
    @Autowired
    private MOrderMapper mOrderMapper;
    @Autowired
    private MGuestServiceImpl mGuestService;
    @Autowired
    private MGuestMapper mGuestMapper;
    @Autowired
    private MDeviceCategoryServiceImpl mDeviceCategoryService;


    /**
     * 获取所有客户信息
     * @return
     */
    @ApiOperation(value = "所有客户",notes = "维保人员查看所有客户")
    @GetMapping("/guest")
    public R getAllGuest(){
        List<MGuest> guests = mGuestService.getAll();
        Map<String, Object> map = new HashMap<>();
        map.put("allGuestAndDev",guests);
        return R.ok(map);
    }




    /**
     * 获取所有用户信息以及所有设备类别信息
     * @return
     */
    @ApiOperation(value = "所有客户及所有设备类别")
    @GetMapping("/guest-dev")
    public R getAllGuestAndDev(){

        Map<String, List> allGuestAndDev = mOrderService.getAllGuestAndDev();
//        return new Result(ResponseEnum.SUCCESS,allGuestAndDev);
        Map<String, Object> map = new HashMap<>();
        map.put("allGuestAndDev",allGuestAndDev);
        return R.ok(map);
    }

    /**
     * 插入客户数据，根据是否成功返回不同结果
     * @param mGuest
     * @return
     */
    @PostMapping("/guest-submit")
    @ApiOperation(value = "新建客户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType ="path",name = "company",value = "公司",required = true,dataType = "String"),
            @ApiImplicitParam(paramType ="path",name = "attn",value = "联系人",required = true,dataType = "String"),
            @ApiImplicitParam(paramType ="path",name = "phoneNumber",value = "电话",required = true,dataType = "String"),
            @ApiImplicitParam(paramType ="path",name = "address",value = "地址",required = true,dataType = "String")
    })
    public R insertGuest(@RequestBody MGuest mGuest){
        boolean b = mGuestService.insertGuest(mGuest);
        return isOk(b);//没有详细信息提示
    }

    /**
     * 提交新的设备类型
     * @param deviceCategory
     * @return
     */
    @PostMapping("/dev-cate-submit")
    public R insertDevCate(@RequestBody String deviceCategory){

        boolean b = mDeviceCategoryService.insertDevCate(deviceCategory);
        return isOk(b);//没有详细提示
    }

    /**
     * 提交订单
     * @param mOrder
     * @return
     */
    @PostMapping("/submit")
    public R submitOrder(@RequestBody MOrder mOrder){

        boolean b = mOrderService.insertOrder(mOrder);
        return isOk(b);//没有详细提示
    }

    /**
     * 获取所有订单
     * @return
     */
    @GetMapping("/all")
    public R selectAllOrder(){
        List<MOrder> allOrder = mOrderService.getAllOrder();
        Map<String, Object> map = new HashMap<>();
        map.put("allOrder",allOrder);
        return R.ok(map);
    }

    /**
     * 通过客户(公司)查询订单
     * @param guestId
     * @return
     */
    @GetMapping("/by-gId/{guestId}")
    public R selectOrderByCompany(@PathVariable("guestId") String guestId){

//        Map<String, Object> columnMap = Maps.newHashMap();
//        columnMap.put("guest_name", guestName);
//        List<MGuest> guestsByName = mGuestMapper.selectByMap(columnMap);

//        /*
//        根据用户名查询该用户信息
//         */
//        QueryWrapper<MGuest> guestQueryWrapper = new QueryWrapper<MGuest>();
//        guestQueryWrapper.like("company", company);
//        MGuest mGuest = mGuestMapper.selectOne(guestQueryWrapper);
//
        /*
        根据用户id查询订单
         */
        QueryWrapper<MOrder> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.like("guest_id",guestId);
        List<MOrder> orderListByGuest = mOrderMapper.selectList(orderQueryWrapper);


        /*
        缺少异常判断
         */
        Map<String, Object> map = new HashMap<>();
        map.put("orderListByGuest",orderListByGuest);
        return R.ok(map);
    }


    /**
     * 设备类别id查询订单
     * @param devCateId
     * @return
     */
    @GetMapping("/by-dev/{devCateId}")
    public R selectOrderByDevCate(@PathVariable("devCateId") Integer devCateId){


        /*
        根据设备类别id查询订单
         */
        QueryWrapper<MOrder> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.like("dev_cate_id",devCateId);
        List<MOrder> orderListByDevCateId = mOrderMapper.selectList(orderQueryWrapper);


        /*
        缺少异常判断
         */
        Map<String, Object> map = new HashMap<>();
        map.put("orderListByGuest",orderListByDevCateId);
        return R.ok(map);
    }


    /**
     * 工程师查询订单
     * @param engineerName
     * @return
     */
    @GetMapping("/by-eng/{engineerName}")
    public R selectOrderByEngineer(@PathVariable("engineerName") String engineerName){


        /*
        根据设备类别id查询订单
         */
        QueryWrapper<MOrder> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.like("engineer_name",engineerName);
        List<MOrder> orderListByEngineer = mOrderMapper.selectList(orderQueryWrapper);


        /*
        缺少异常判断
         */
        Map<String, Object> map = new HashMap<>();
        map.put("orderListByEngineer",orderListByEngineer);
        return R.ok(map);
    }


    /**
     * 对是否插入数据成功做判断 返回不同的R
     * @param b
     * @return
     */
    public static R isOk(boolean b){

        if(b){
            return R.ok();
        }else {
            return R.error();
        }
    }
}
